<?php

/**
 *  語系管理
 *   
 *   
 */
class LocaleManager
{
    protected $_localeType = 'zh_TW';
    protected $_localePath = '';
    protected $_data = Array();
    
    function __construct()
    {
        $this->_localePath = Yii::app()->getBasePath().'/libraries/locale/locale/';
    }

    public function setLocaleType( $localeType='zh_TW' )
    {
        $this->_localeType = $localeType;
    }

    public function loadLocale( $localePath='' )
    {
        if( $localePath ) {
            $this->_localePath = $localePath;
        }
        $this->_data = include( $this->_localePath .'/locale_'. $this->_localeType .'.php' );
    }


    /**
     *  翻譯 locale
     */
    public function tr( $name )
    {
        $key = strtolower($name);
        if( array_key_exists( $key, $this->_data ) ) {
            return $this->_data[$key];
        }
        else {
            return $key;
        }
    }

    /**
     *  翻譯 locale by model
     */
    public function trm( $modelName, $name )
    {
        return $this->tr("model_{$modelName}_{$name}");
    }

}
